﻿using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using CoreComponents;
using Crowler.Model.CoreComponents;

namespace Crowler.Dispatcher.Api.Di
{
    public class MyInstanceProvider : IInstanceProvider, IContractBehavior
    {
        private readonly IAppEnvironment _dependency;

        public MyInstanceProvider(IAppEnvironment dependency)
        {
            if (dependency == null)
            {
                throw new ArgumentNullException("dependency");
            }

            _dependency = dependency;
        }
        public object GetInstance(InstanceContext instanceContext)
        {
            return GetInstance(instanceContext, null);
        }

        public object GetInstance(InstanceContext instanceContext, Message message)
        {
            return new DispatcherApi(_dependency);
        }

        public void ReleaseInstance(InstanceContext instanceContext, object instance)
        {

        }

        public void Validate(ContractDescription contractDescription, ServiceEndpoint endpoint)
        {

        }

        public void ApplyDispatchBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, DispatchRuntime dispatchRuntime)
        {
            dispatchRuntime.InstanceProvider = this;
        }

        public void ApplyClientBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, ClientRuntime clientRuntime)
        {

        }

        public void AddBindingParameters(ContractDescription contractDescription, ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
        {

        }
    }
}
